package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ：zhao
 * @date ：Created in 2020/1/3 0003
 * @description ：菜单实现类
 * @version: 1.0
 */
@Service
@Slf4j
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Override
    public List<Menu> findAll() {
        return menuDao.selectAll();
    }

    @Override
    public void add(Menu menu) {
        menuDao.insert(menu);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Menu> page = menuDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void delete(Integer id) {
        //校验是否可以删除
        Long count = menuDao.countRoleByMenuId(id);
        if(count >0){
            throw new BusinessRuntimeException("当前菜单关联相关角色，不能删除");
        }
        //实际删除操作
        menuDao.deleteById(id);

    }

    @Override
    public void edit(Menu menu) {
        menuDao.update(menu);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.selectById(id);
    }

}
